#pragma once

#include "WndSystemMenu.h"
#include "WndMenuButton.h"
#include "WndMap.h"
#include "WndPlayer.h"
#include "WndTab.h"
#include "WndListTime.h"
#include "WndListEvent.h"
#include "WndVolume.h"
#include "WndSpeedmeter.h"
#include "WndGSensorGraph.h"
#include "WndGSensor.h"
#include "WndGps.h"
#include "WndPbSpeed.h"
#include "WndSearchOpt.h"
#include "WndTimeBar.h"
#include "WrapMap.h"
#include "WndLogo.h"
#include "DlgViewerBG.h"
#include "../_inc_app/WndDisplay.h"
#include "../_inc_app/DxSoundEx.h"
#include "../_inc_app/IIntrReplayEx.h"

#include "../_inc_app/CtrlButton.h"

#include "StatusMgr.h"

#include "DlgBackup.h"
#include "DlgColor.h"

#include "ExtScreen3.h"
#include "FileOpenProgress.h"
#include "ListMgr.h"

#include "../_inc_set/def_setting.h"
#include "../_inc_app/SplashScreenEx.h"


#if 1 /* jhbae */
#include "../_inc_util/AviParser.h"
#endif


#define TIMER_ID_RESETDISPLAY		0x8100

// CUIMain

class CUIMain : public CWnd
{
	DECLARE_DYNAMIC(CUIMain)

public:
	enum 
	{
		MAINSCREEN = 0,
		SUBSCREEN,
		MAX_SCREEN_COUNT
	};

#ifdef
public:
	CAviParser	*m_pAviParser;
#endif

private:
	CWndDisplay			*m_pWndScreen;
	CWndDisplay			*m_pWndSubScreen;
	CWndDisplay			*m_pWndDisplay[MAX_SCREEN_COUNT];
	CDxSoundEx			*m_pDxSoundEx;
	CWndMenuButton		m_WndMenuButton;
	CWndPlayer			m_WndPlayer;
	CWndTab				m_WndTab;
	CWndListTime		m_WndListTime;
	CWndListEvent		m_WndListEvent;
	CWndSystemMenu		m_WndSystemMenu;
	CWndVolume			m_WndVolume;
	CWndSpeedmeter		m_WndSpeedmeter;
	CWndGSensor			m_WndGSensor;
	CWndGps				m_WndGps;
	CWndPbSpeed			m_pPbSpeed;
	CWndGSensorGraph	m_WndGSensorGraph;
	CWndSearchOpt		m_WndSearchOpt;
	CWndTimeBar			m_WndTimeBar;
	CWndLogo			m_WndLogo;
	CListMgr			*m_pListMgr;


	IIntrReplayEx		*m_pIntrPlayEx;

	CWndMap*			m_pWndMap;
	CWrapMap			m_WrapMap;
	
	CExtScreen3			m_ExtScreen3;

	CCtrlButton			m_btnOpen;
	CCtrlButton			m_btnDelete;
	CToolTipCtrl		m_ToolTipCtrl;

	CStatusMgr*			m_pStatusMgr;
	CFileOpenProgress	m_DlgFileOpen;

	int					m_nTabIndex;
	BOOL				m_bInit;

	// Volume 
	DWORD				m_dwVolume;	

	//
	BOOL				m_bFullScreen;

	int					m_nPopupCamera;
	BOOL				m_bPlayOn;
	SEARCH_TYPE			m_nSearchType;

	int				m_nWndGpsSize;

	//////////////////////////////////////////////////////////////////////////
	//UI
	SIZE m_csScreen, m_csScrFull;
	SIZE m_csDisplay[MAX_SCREEN_COUNT];
	BOOL m_bMaximize;
	BOOL m_bFull;
	int m_nCurWinWidth, m_nCurWinHeight;
	int m_nFullScrChannel;
	BOOL m_bSwapScreen;
	DWORD m_dwRatioMode;
	
	//** Video : Reverse
	BYTE  m_oReverse[MAX_SCREEN_COUNT];
	BOOL  m_bUpDown[MAX_SCREEN_COUNT];
	BOOL  m_bLeftRight[MAX_SCREEN_COUNT];

	CRect m_rtRestoreMax;
	CRect m_rtRestoreFull;

	BOOL m_bSwapMove;
	int m_nSwapHold;

	BOOL m_bAskRecovery;
	BOOL m_bAutoRearrange;

	BOOL m_bFileOpen;
	BOOL m_bMouseDown;
	BOOL m_bMoveFile;

	CStringArray m_sarrText;
	CStringArray m_sarrMessage;

	ST_IMAGE_INFO m_stImageInfo;

	ST_FILE_NAME_FORMAT m_stFileNameFormat;
	LPST_FILE_NAME_FORMAT m_lpstFileNameFormat;
	int m_nCurrentFileFormat;

	CCriticalSection m_csResetDisplay;
	BOOL m_bLoadUserData;
	BOOL m_bDisplayChange;

	int m_nPlayerState;

	CEvent *m_pEtShared;
	CEvent *m_pEtToSetup;
	HANDLE m_hMap;
	BYTE *m_pSharedMemSetup;
	BOOL m_bSharedThread;
	HANDLE m_hSharedThread;

	CSplashScreenEx *m_pSplashScreen;

	int m_nViewerMode;
	

public:
	CDlgBackup			m_DlgBackup;
	CDlgColor			*m_pDlgColor;
	CDlgViewerBG		m_dlgViewerSetup;

public:
	CUIMain();
	virtual ~CUIMain();

	void StoppedUi();
	void CloseIndex();
	void Resize2(int cx, int cy);
	void ResizeServiceMenu(int cx, int cy);
	void Refresh();
	void EnlargeView(BOOL bLarge);
	void OnSetup();

	void DeleteSelectedFiles();
	IIntrReplayEx *GetIntrReplayExObject() { return m_pIntrPlayEx; };
	void ScreenControl();

	BOOL GetMaximized() { return m_bMaximize; };
	BOOL GetFullScreen() { return m_bFull; };

	void RestoreSizeMaximized(CRect rtWnd) { m_rtRestoreMax = rtWnd; };
	void RestoreSizeFull(CRect rtWnd) { m_rtRestoreFull = rtWnd; };

	void ShowLogo();
	void SetMapConnection(BOOL bState);
	BOOL GetPlayStatus() { return m_bPlayOn; };
	void SetPopupCamera(int nChannel);

	void OpenRecFile();
	BOOL AskRecoveryIndexTime(int nCurList);
	BOOL AskRecoveryIndexTimeP(int nCurList);
	void PostRecoveryIndexTime(int nCurList);
	void DoRecoveryIndexTime(int nCurList);

	BOOL AskRecoveryIndexEvent(int nCurList);
	BOOL AskRecoveryIndexEventP(int nCurList);
	void PostRecoveryIndexEvent(int nCurList);
	void DoRecoveryIndexEvent(int nCurList);

	int AskRecoveryFileOpen();
	void DoRecoveryFileOpen();

	void SetFileOpenProgressStart(int nTotalLength);
	void SetFileOpenProgress(int nPos);
	void SetFileOpenProgressInc();
	void SetFileOpenProgressEnd();

	CListMgr *GetListMgr() { return m_pListMgr; };
	void GetAviFileNameFormat(LPST_AVI_NAME_FORMAT lpstAviNameFormat);
	void GetAviFileNameFormatList(int nIndex, LPST_FILE_NAME_FORMAT lpstFmt);
	void GetAviFileNameFormatDefault(LPST_FILE_NAME_FORMAT lpstFmt);

	void GetImageData(LPVOID pImageData);
	void GetAudioData(LPVOID pAudioData);
	void GetUserData(LPVOID pUserData);
	void GetUserInfo(LPVOID pUserInfo);
	void TimeBarLoad(LPST_TIMEBAR_LOAD_DATA lpData);
	void PlayButtonControl(BOOL bEnable);

	//display
	void DisplayVideoStream(LPST_DISPLAY_VIDEO_STREAM lpStream);
	void DisplayUpdate(int nChannel);

	//audio
	void PlayAudioData(LPST_PLAY_AUDIO_DATA lpData);
	BOOL PlayAudioLoadStatus();
	void PlayAudioStop(); 


	//////////////////////////////////////////////////////////////////////////
	//CtlMessage
	void Exit(BOOL bRestart = FALSE);
	void MaxScreen();
	void FullScreen();
	void PrintReport();
	void OnBackup();
	void OnViewerSetupEx();
	void OnTabTime();
	void OnTabEvent();
	void OnShowGPSPanel();

	//////////////////////////////////////////////////////////////////////////
	//PlbMessage
	void PlayForward();
	void PlayBackward();
	void Pause();
	void PlayStop();
	void PlayStopReplay(LPST_NOTIFY_STOP lpstNotifyStop);
	void StepForward();
	void StepForwardKeyframe();
	void StepBackward();
	void StepBackwardKeyframe();
	void NextFile();
	void PrevFile();
	void SetVolume(int nVolume);
	void SearchEvent(DWORD dwRecType);
	void PlayNPause();
	void StopNPlay();

	//////////////////////////////////////////////////////////////////////////
	//DXSMessage
	void SetZoom(LPST_WHEEL_INFO lpstWheelInfo);

	//////////////////////////////////////////////////////////////////////////
	void SetZoomDefault();
	void SetZoomPIP();

	//////////////////////////////////////////////////////////////////////////
	void SetVideoReverse();

	//////////////////////////////////////////////////////////////////////////
	CWndTab *GetWndTab() { return &m_WndTab; };
	CWndPbSpeed *GetWndPbSpeed() { return &m_pPbSpeed; };

private:
	BOOL Initialize();
	void Release();
	void LoadCursorWaiting();
	void LoadCursorArrow();
	void GetMonitorRect(HWND hWnd, CRect *rcMonitor, BOOL bWorkArea = FALSE);

	BOOL CreateDisplay();

	BOOL CreateDxSound();
	void DeleteDxSound();

	void LoadText();
	BOOL ResetDisplay();

	void InitSharedMemory();
	void ExitSharedMemory();

	void OnApplySetup();
	void RefreshSkin();


protected:
	static void CallbackReplay(UINT nCmd, DWORD wParam, void* lParam, void* pApp);
	static void CallbackMap(UINT nCmd, DWORD wParam, void *lParam, void *pApp);

	static UINT __stdcall _procSharedMemory(LPVOID lpParam);
	void procSharedMemory();

protected:
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnPaint();
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg long OnCtlMessage(UINT wParam, LONG lParam);
	afx_msg long OnPlbMessage(UINT wParam, LONG lParam);
	afx_msg long OnDxsMessage(UINT wParam, LONG lParam);
	afx_msg LRESULT OnApplyViewerSetup(WPARAM wParam, LPARAM lParam);
	afx_msg void OnPopupScreen();
	//afx_msg long OnRefreshSkin(UINT wParam, LONG lParam);
	afx_msg LRESULT OnRefreshSkin(WPARAM wParam, LPARAM lParam);
	virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
	DECLARE_MESSAGE_MAP()
public:
		
	virtual BOOL PreTranslateMessage(MSG* pMsg);
	afx_msg void OnPopupSwapView();
	afx_msg void OnPopupOneView();
	afx_msg void OnRatio16to09();
	afx_msg void OnRatio04to03();
	afx_msg void OnRatioOriginal();
	afx_msg void OnRatioDefault();
	afx_msg void OnRatioScale();
	afx_msg void OnPopupColorDlg();
	afx_msg void OnZoomDefault();
	afx_msg LRESULT OnNcHitTest(CPoint point);
	
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnTimer(UINT_PTR nIDEvent);
	afx_msg void OnVideoLeftRight();
	afx_msg void OnVideoUpDown();
};

enum 
{
	Message_0 = 0,
	Message_1,
	Message_2,
	Message_3,
	Message_4,
	Message_5,
	Message_7_0,
	Message_7_1,
	Message_7_2,
	Message_7_3,
	Message_7_4,
	Message_8,
	Message_9_0,
	Message_9_1,
	Message_10_0,
	Message_10_1,
	Message_10_2,
	Message_11_0,
	Message_11_1,
	Message_12,
};
